﻿using Newtonsoft.Json;
using NPOI.OpenXmlFormats.Dml.Diagram;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaseUi
{
    /// <summary>
    /// 默认数据库
    /// </summary>
    public static class DataDb
    {
        public static SqlSugarScope db = new SqlSugarScope(new ConnectionConfig()
        {
            ConnectionString = LocalFile.Config.MySql,
            DbType = DbType.MySqlConnector,//[MySqlConnector]安装[SqlSugar.MySqlConnector]+[MySqlConnector];[MySql]安装[MySql.Data];
            IsAutoCloseConnection = true
        }, db =>
        {
            db.Aop.OnError = ex =>
            {
                Logs.Write($@"{nameof(DataDb)}{Environment.NewLine}SQL:{ex?.Sql}{Environment.NewLine}Parametres:{JsonConvert.SerializeObject(ex?.Parametres)}{Environment.NewLine}InnerException:{ex?.InnerException?.ToString()}{Environment.NewLine}Exception:{ex?.ToString()}{Environment.NewLine}", LogsType.DbErr);
            };
        });
        /// <summary>
        /// 测试记录
        /// </summary>
        public static SqlSugarScope dbBox = new SqlSugarScope(new ConnectionConfig()
        {
            ConnectionString = $"Data Source={Path.Combine(LocalFile.DataDir, "datanum.db3")};Version=3;journal_mode=WAL;",
            DbType = DbType.Sqlite,//[Sqlite]安装[System.Data.SQLite];
            IsAutoCloseConnection = true
        }, db =>
        {
            db.Aop.OnError = ex =>
            {
                Logs.Write($@"{nameof(dbBox)}{Environment.NewLine}SQL:{ex?.Sql}{Environment.NewLine}Parametres:{JsonConvert.SerializeObject(ex?.Parametres)}{Environment.NewLine}InnerException:{ex?.InnerException?.ToString()}{Environment.NewLine}Exception:{ex?.ToString()}{Environment.NewLine}", LogsType.DbErr);
            };
        });/// <summary>
           /// 测试记录
           /// </summary>
        public static SqlSugarScope dbBoxs = new SqlSugarScope(new ConnectionConfig()
        {
            ConnectionString = $"Data Source={Path.Combine(LocalFile.DataDir, "datanums.db3")};Version=3;journal_mode=WAL;",
            DbType = DbType.Sqlite,//[Sqlite]安装[System.Data.SQLite];
            IsAutoCloseConnection = true
        }, db =>
        {
            db.Aop.OnError = ex =>
            {
                Logs.Write($@"{nameof(dbBoxs)}{Environment.NewLine}SQL:{ex?.Sql}{Environment.NewLine}Parametres:{JsonConvert.SerializeObject(ex?.Parametres)}{Environment.NewLine}InnerException:{ex?.InnerException?.ToString()}{Environment.NewLine}Exception:{ex?.ToString()}{Environment.NewLine}", LogsType.DbErr);
            };
        });
        /// <summary>
        /// 初始化数据
        /// </summary>
        public static void Inidata()
        {
            //不存在创建数据库，存在不会创建
            dbBox.DbMaintenance.CreateDatabase();
            //创建表根据实体类
            dbBox.CodeFirst.InitTables(typeof(ZhiKongInfo));
            //初始化数据
            if (!dbBox.Queryable<ZhiKongInfo>().Any())
            {
                var dt = DateTime.Now;
                var data = new ZhiKongInfo()
                {
                    Code = "0001",
                    State = "通过",
                    //State1 = "通过",
                    restul = "0001",
                    //restul1 = "0001",
                    SN = "0001",
                    //Type = "0001",
                    TestOS_Rev = "0001",
                    BT_MAC = "0001",
                    BT_NAME = "0001",
                    //PackageWorker = "0001",
                    CPU = "0001",
                    //PackageTime = dt,
                    Time = dt,
                    //Time1 = dt,
                };
                if (data.State1 is null)
                {
                    data.State1="";
                }
                if (data.restul1 is null)
                {
                    data.restul1="";
                }
                if (data.Station is null)
                {
                    data.Station="";
                }
                if (data.Station1 is null)
                {
                    data.Station1="";
                }
                if (data.PackageWorker is null)
                {
                    data.PackageWorker="";
                }
                if (data.PackageTime is null)
                {
                    data.PackageTime=dt;
                }
                if (data.Time1 == null)
                {
                    data.Time1=dt;
                }

                dbBox.Insertable(data).ExecuteCommand();

            }
            //不存在创建数据库，存在不会创建
            dbBoxs.DbMaintenance.CreateDatabase();
            //创建表根据实体类
            dbBoxs.CodeFirst.InitTables(typeof(ZhiKongInfos));
            //初始化数据
            if (!dbBoxs.Queryable<ZhiKongInfos>().Any())
            {
                var dt = DateTime.Now;
                var data = new ZhiKongInfos()
                {
                    SN = "",
                    Code = "",
                    BT_MAC = "",
                    CPU = "",
                    BT_NAME = "",
                    Station = "",
                    TestOS_Rev = "",
                    State = "",
                    restul = "",
                    Time =  dt,
                };
                dbBoxs.Insertable(data).ExecuteCommand();

            }
        }
    }
}
